package com.sysmanagment.controller.user;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.security.management.IdentityManager;

import com.sysmanagment.manager.user.UserManager;
import com.sysmanagment.model.user.Role;
import com.sysmanagment.model.user.User;

@Name(value = "userController")
public class UserController {
	
	@In(value="userManager",create=true)
	UserManager userManager;
	
	
	User user=new User();
	
	Role selectedRole=new Role();
	
	public void saveUser(){
		IdentityManager identityManager = IdentityManager.instance();
		identityManager.createUser(user.getUsername(), user.getPasswordHash(), user.getFirstname(), user.getLastname());
//		identityManager.grantRole(name, role);
		
//		List<Role> r = new ArrayList<Role>();
//		r.add(getSelectedRole());
//		user.setRoles(r);
//		userManager.saveUser(user);
	}
	
	public void createRole(){
		IdentityManager identityManager = IdentityManager.instance();
		identityManager.createRole(selectedRole.getRolename());
	}
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	

	public Role getSelectedRole() {
		return selectedRole;
	}

	public void setSelectedRole(Role selectedRole) {
		this.selectedRole = selectedRole;
	}

	public List<Role> getRoles(){
		List<SelectItem> items = new ArrayList<SelectItem>();
		List<Role> allRoles = userManager.getAllRoles();
		for (Role role : allRoles) {
			items.add( new SelectItem(role.getId(),role.getRolename()));
		}
		return allRoles;
	}
}
